import type { 
    SelectOption, 
    RuleScenariosQueryParams, 
    RuleScenariosSave, 
    RuleScenariosInfo 
} from "@/types"
import { reactive, ref } from "vue"
import axios from "axios"
import { ElMessage } from "element-plus";

export default function() {
    let insertRuleScenariosWindowEnable = ref(false)
    let updateRuleScenariosWindowEnable = ref(false)
    let ruleScenariosCurrentPage = ref(1)
    let ruleScenariosPageSize = ref(10)
    let ruleScenariosQueryParams = reactive<RuleScenariosQueryParams>({
        scenariosKey: '',
        scenariosName: '',
        isEnable: 0
    })
    let insertRuleScenarios = reactive<RuleScenariosSave>({
        id: '',
        scenariosKey: '',
        scenariosName: '',
        conditionModelName: '',
        resultModelName: '',
        description: '',
        isEnable: 0
    })
    let updateRuleScenarios = reactive<RuleScenariosSave>({
        id: '',
        scenariosKey: '',
        scenariosName: '',
        conditionModelName: '',
        resultModelName: '',
        description: '',
        isEnable: 0
    })
    // 规则场景信息集合
    let ruleScenariosInfoList = reactive<RuleScenariosInfo[]>([])
    // 业务模型选择框
    let businessModelOptionList = reactive<SelectOption[]>([])
    async function searchRuleScenariosByQueryParam(params: RuleScenariosQueryParams) {
        console.log('搜索规则场景')
        console.log(JSON.stringify(params))
        let result = await axios.post("/ruleScenarios/selectList/byQuery", params).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ruleScenariosInfoList.length = 0
                Object.assign(ruleScenariosInfoList, res.data.content)
                ruleScenariosCurrentPage.value = 1
            } else {
                ElMessage({
                    type: 'error',
                    message: res.data.msg
                });
            }
        })
    }
    async function updateRuleScenariosInfoList() {
        let result = await axios.post("/ruleScenarios/selectList/byQuery", ruleScenariosQueryParams).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ruleScenariosInfoList.length = 0
                Object.assign(ruleScenariosInfoList, res.data.content)
            } else {
                ElMessage({
                    type: 'error',
                    message: res.data.msg
                });
            }
        })
    }
    async function deleteRuleScenarios(id: number) {
        console.log('调用后端接口删除规则场景')
        await axios.get("/ruleScenarios/delete/" + id).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ElMessage({
                    type: 'success',
                    message: '删除规则场景成功!'
                });
            } else {
                ElMessage({
                    type: 'error',
                    message: res.data.msg
                })
            }
        })
        // 更新列表
        updateRuleScenariosInfoList()
    }
    // 自动补全框查询函数
    function autoCompleteQuerySearch(queryString: string, cb: any) {
        console.log('自动补全框查询函数')
        console.log(JSON.stringify(businessModelOptionList))
        console.log(queryString)
        let results = queryString
        ? businessModelOptionList.filter(autoCompleteFilter(queryString))
        : businessModelOptionList
        // call callback function to return suggestions
        cb(results)
    }
    // 自动补全框过滤函数
    function autoCompleteFilter(queryString: string) {
        return (selectOption: SelectOption) => {
            return (
                selectOption.key.toLowerCase().indexOf(queryString.toLowerCase()) === 0
            )
        }
    }
    // 自动补全框选择事件
    function updateRuleScenariosAutoCompleteConditionModelHandleSeletct(item: Record<string, any>) {
        console.log('autoCompleteHandleSeletct')
        console.log(JSON.stringify(item))
        updateRuleScenarios.conditionModelName = item.key
    }
    function updateRuleScenariosAutoCompleteResultModelHandleSeletct(item: Record<string, any>) {
        console.log('autoCompleteHandleSeletct')
        console.log(JSON.stringify(item))
        updateRuleScenarios.resultModelName = item.key
    }
    function insertRuleScenariosAutoCompleteConditionModelHandleSeletct(item: Record<string, any>) {
        console.log('autoCompleteHandleSeletct')
        console.log(JSON.stringify(item))
        insertRuleScenarios.conditionModelName = item.key
    }
    function insertRuleScenariosAutoCompleteResultModelHandleSeletct(item: Record<string, any>) {
        console.log('autoCompleteHandleSeletct')
        console.log(JSON.stringify(item))
        insertRuleScenarios.resultModelName = item.key
    }
    // 自动补全框处理事件函数
    function actoCompleteHandleIconClick(event: Event) {
        console.log('actoCompleteHandleIconClick')
        console.log(event)
    }
    function loadBusinessModelOptionList() {
        // 调用后端接口
        console.log('加载业务模型')
        getAllBusinessModelOption()
        console.log(JSON.stringify(businessModelOptionList))
    }
    // 获取所有业务模型
    async function getAllBusinessModelOption() {
        try {
            let result = await axios.get("/businessModel/get/all/businessModel/options");
            console.log('获取自定义类型option集合')
            if (result.data.code == 200) {
                businessModelOptionList.length = 0
                Object.assign(businessModelOptionList, result.data.content)
            }
        } catch (error) {
            console.log(error)
        }
    }
    async function insertRuleScenariosOnSubmit(params:RuleScenariosSave) {
        console.log('调用后端接口插入规则场景')
        console.log(JSON.stringify(params))
        let result = await axios.post("/ruleScenarios/insert", params).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ElMessage({
                    type: 'success',
                    message: '新增规则场景成功!'
                });
                // 清空参数
                Object.assign(insertRuleScenarios, {
                    id: '',
                    scenariosKey: '',
                    scenariosName: '',
                    conditionModelName: '',
                    resultModelName: '',
                    description: '',
                    isEnable: 0
                })
                // 更新列表
                updateRuleScenariosInfoList()
                // 关闭窗口
                insertRuleScenariosWindowEnable.value = false
            } else {
                ElMessage({
                    type: 'error',
                })

            }
        })
    }
    // 修改规则场景
    function updateRuleScenariosInfo(index: number, row: RuleScenariosInfo) {
        console.log('修改规则场景')
        // 赋初始值
        Object.assign(updateRuleScenarios, row)
        // 打开窗口
        updateRuleScenariosWindowEnable.value = true
    }
    async function updateRuleScenariosOnSubmit(params:RuleScenariosSave) {
        console.log('调用后端接口更新规则场景')
        console.log(JSON.stringify(params))
        let result = await axios.post("/ruleScenarios/update", params).then(res => {
            console.log(res)
            if (res.data.code == 200) {
                ElMessage({
                    type: 'success',
                    message: '更新规则场景成功!'
                });
                // 清空参数
                Object.assign(updateRuleScenarios, {
                    id: '',
                    scenariosKey: '',
                    scenariosName: '',
                    conditionModelName: '',
                    resultModelName: '',
                    description: '',
                    isEnable: 0
                })
                // 更新列表
                updateRuleScenariosInfoList()
                // 关闭窗口
                updateRuleScenariosWindowEnable.value = false
            } else {
                ElMessage({
                    type: 'error',
                })

            }
        })
    }
    // 明细项分页处理页码
    function ruleScenariosHandleCurrentChange(param: number) {
        ruleScenariosCurrentPage.value = param
    }
    // 明细项处理每页条数变化
    function ruleScenariosHandleSizeChange(param: number) {
        ruleScenariosPageSize.value = param
        ruleScenariosCurrentPage.value = 1
    }
    return {
        insertRuleScenariosWindowEnable,
        updateRuleScenariosWindowEnable,
        ruleScenariosQueryParams,
        insertRuleScenarios,
        updateRuleScenarios,
        ruleScenariosInfoList,
        ruleScenariosCurrentPage,
        ruleScenariosPageSize,
        searchRuleScenariosByQueryParam,
        deleteRuleScenarios,
        insertRuleScenariosOnSubmit,
        updateRuleScenariosOnSubmit,
        autoCompleteQuerySearch,
        actoCompleteHandleIconClick,
        loadBusinessModelOptionList,
        updateRuleScenariosInfo,
        ruleScenariosHandleCurrentChange,
        ruleScenariosHandleSizeChange,
        insertRuleScenariosAutoCompleteConditionModelHandleSeletct,
        insertRuleScenariosAutoCompleteResultModelHandleSeletct,
        updateRuleScenariosAutoCompleteConditionModelHandleSeletct,
        updateRuleScenariosAutoCompleteResultModelHandleSeletct
    }
}